﻿using System.Web;
using Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ESPWebPartsUnitTest
{

    /// <summary>
    ///This is a test class for NavigationHelperTest and is intended
    ///to contain all NavigationHelperTest Unit Tests
    ///</summary>
    [TestClass]
    public class NavigationHelperTest
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        ///A test for SetParameter if parameter already exist
        ///</summary>
        [TestMethod]
        public void AddParameterWithExistingParametersTest()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx","k=1&a=b");

            string newUrl = NavigationHelper.SetParameter(new NavigationHelperParameter(request), "myparameter", "myvalue", ESPSearchGroupEnum.Group0);
            string expectedurl = "/test.aspx?k=1&a=b&myparameter=myvalue";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist
        ///</summary>
        [TestMethod]
        public void AddParameterWithNoParametersTest()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "");

            string newUrl = NavigationHelper.SetParameter(new NavigationHelperParameter(request), "myparameter", "myvalue", ESPSearchGroupEnum.Group0);
            string expectedurl = "/test.aspx?myparameter=myvalue";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterWithNoParametersEncodingTest()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "");
            string newUrl = NavigationHelper.SetParameter(new NavigationHelperParameter(request), "myparameter", "my&value", ESPSearchGroupEnum.Group0);
            string expectedurl = "/test.aspx?myparameter=my%26value";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterWithPrefixWithParametersEncodingTest()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "k=test&a=b");
            string newUrl = NavigationHelper.SetParameter(new NavigationHelperParameter(request), "myparameter", "my&value", ESPSearchGroupEnum.Group1);
            string expectedurl = "/test.aspx?k=test&a=b&myparameter_1=my%26value";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterReplacingProperty()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "myparameter_1=my%26value");
            string newUrl = NavigationHelper.SetParameter(new NavigationHelperParameter(request), "myparameter", "my&value", ESPSearchGroupEnum.Group1);
            string expectedurl = "/test.aspx?myparameter_1=my%26value";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterReplacingWithParameterProperty()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "k=test&myparameter_1=my%26value&a=b");
            string newUrl = NavigationHelper.SetParameter(new NavigationHelperParameter(request), "myparameter", "my&value", ESPSearchGroupEnum.Group1);
            string expectedurl = "/test.aspx?k=test&myparameter_1=my%26value&a=b";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        /// Tests for NavigationHelper.RemoveParameter.
        /// </summary>
        [TestMethod]
        public void TestRemoveParameter()
        {
            // Nothing should be changed when unknown param is asked to be removed
            DoRemoveTest("foo=bar&baz=true", "nosuchparam", "somevalue", "/test.aspx?foo=bar&baz=true");

            // Remove first, middle, and last parameters
            DoRemoveTest("foo=bar&baz=true&jalla=no", "foo", "bar", "/test.aspx?baz=true&jalla=no");
            DoRemoveTest("foo=bar&baz=true&jalla=no", "baz", "true", "/test.aspx?foo=bar&jalla=no");
            DoRemoveTest("foo=bar&baz=true&jalla=no", "jalla", "no", "/test.aspx?foo=bar&baz=true");

            // Ensure complete match is requried to remove term
            string fullUrl = "/test.aspx?foo=bar&baz=true&jalla=no";
            DoRemoveTest("foo=bar&baz=true&jalla=no", "oo", "bar", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "foo", "ba", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "az", "true", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "baz", "tr", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "alla", "no", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "jalla", "n", fullUrl);
        }

        private static void DoRemoveTest(string query, string removeName, string removeValue, string expectedUrl)
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", query);
            string newUrl = NavigationHelper.RemoveParameter(new NavigationHelperParameter(request), removeName, removeValue, ESPSearchGroupEnum.Group0);
            Assert.AreEqual(expectedUrl, newUrl);
        }



        // Tests using the NavigationHelperParameter class as property

        /// <summary>
        ///A test for SetParameter if parameter already exist
        ///</summary>
        [TestMethod]
        public void AddParameterWithExistingParametersTest2()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "k=1&a=b");
            NavigationHelperParameter navParam = new NavigationHelperParameter(
                request.RawUrl, request.QueryString, request.FilePath);
            string newUrl = NavigationHelper.SetParameter(navParam, "myparameter", "myvalue", ESPSearchGroupEnum.Group0);
            string expectedurl = "/test.aspx?k=1&a=b&myparameter=myvalue";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist
        ///</summary>
        [TestMethod]
        public void AddParameterWithNoParametersTest2()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "");
            NavigationHelperParameter navParam = new NavigationHelperParameter(
               request.RawUrl, request.QueryString, request.FilePath);
            string newUrl = NavigationHelper.SetParameter(navParam, "myparameter", "myvalue", ESPSearchGroupEnum.Group0);
            string expectedurl = "/test.aspx?myparameter=myvalue";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterWithNoParametersEncodingTest2()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "");
            NavigationHelperParameter navParam = new NavigationHelperParameter(
               request.RawUrl, request.QueryString, request.FilePath);
            string newUrl = NavigationHelper.SetParameter(navParam, "myparameter", "my&value", ESPSearchGroupEnum.Group0);
            string expectedurl = "/test.aspx?myparameter=my%26value";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterWithPrefixWithParametersEncodingTest2()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "k=test&a=b");
            NavigationHelperParameter navParam = new NavigationHelperParameter(
               request.RawUrl, request.QueryString, request.FilePath);
            string newUrl = NavigationHelper.SetParameter(navParam, "myparameter", "my&value", ESPSearchGroupEnum.Group1);
            string expectedurl = "/test.aspx?k=test&a=b&myparameter_1=my%26value";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterReplacingProperty2()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "myparameter_1=my%26value");
            NavigationHelperParameter navParam = new NavigationHelperParameter(
               request.RawUrl, request.QueryString, request.FilePath);
            string newUrl = NavigationHelper.SetParameter(navParam, "myparameter", "my&value", ESPSearchGroupEnum.Group1);
            string expectedurl = "/test.aspx?myparameter_1=my%26value";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        ///A test for SetParameter if no parameter exist plus encoding needed
        ///</summary>
        [TestMethod]
        public void AddParameterReplacingWithParameterProperty2()
        {
            HttpRequest request = new HttpRequest("test", "http://test.com/test.aspx", "k=test&myparameter_1=my%26value&a=b");
            NavigationHelperParameter navParam = new NavigationHelperParameter(
               request.RawUrl, request.QueryString, request.FilePath);
            string newUrl = NavigationHelper.SetParameter(navParam, "myparameter", "my&value", ESPSearchGroupEnum.Group1);
            string expectedurl = "/test.aspx?k=test&myparameter_1=my%26value&a=b";
            Assert.AreEqual(expectedurl, newUrl);
        }

        /// <summary>
        /// Tests for NavigationHelper.RemoveParameter.
        /// </summary>
        [TestMethod]
        public void TestRemoveParameter2()
        {
            // Nothing should be changed when unknown param is asked to be removed
            DoRemoveTest("foo=bar&baz=true", "nosuchparam", "somevalue", "/test.aspx?foo=bar&baz=true");

            // Remove first, middle, and last parameters
            DoRemoveTest("foo=bar&baz=true&jalla=no", "foo", "bar", "/test.aspx?baz=true&jalla=no");
            DoRemoveTest("foo=bar&baz=true&jalla=no", "baz", "true", "/test.aspx?foo=bar&jalla=no");
            DoRemoveTest("foo=bar&baz=true&jalla=no", "jalla", "no", "/test.aspx?foo=bar&baz=true");

            // Ensure complete match is requried to remove term
            string fullUrl = "/test.aspx?foo=bar&baz=true&jalla=no";
            DoRemoveTest("foo=bar&baz=true&jalla=no", "oo", "bar", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "foo", "ba", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "az", "true", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "baz", "tr", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "alla", "no", fullUrl);
            DoRemoveTest("foo=bar&baz=true&jalla=no", "jalla", "n", fullUrl);
        }
    }
}
